package com.zxlspace.java8;

import javax.sql.rowset.CachedRowSet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @ClassName TestJava8Comparator
 * @Description TODO
 * @Author zhangxiaolin@pinpianyi.com
 * @Date 2019/4/11 10:26
 * @Version 1.0
 **/
public class TestJava8Comparator {

    public static void main(String[] args) {
        List<String> stringList = new ArrayList<>();
        stringList.add("Google");
        stringList.add("Runoob");
        stringList.add("TaoBao");
        stringList.add("Tenxun");
        stringList.add("JinDong");
        stringList.add("Sina");
        TestJava8Comparator comparator = new TestJava8Comparator();
        comparator.sortUsingJava7(stringList);
        System.out.println(stringList);

        List<String> stringList1 = new ArrayList<>();
        stringList1.add("Google");
        stringList1.add("Runoob");
        stringList1.add("TaoBao");
        stringList1.add("Tenxun");
        stringList1.add("JinDong");
        stringList1.add("Sina");

        comparator.sortUsingJava8(stringList1);
        System.out.println(stringList1);

        stringList.forEach(System.out::println);
        Car car = new Car("zxl");
//        System.out.println(car::getName);
    }

    //使用java7 排序
    private void sortUsingJava7(List<String> names) {
        Collections.sort(names, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
    }

    private void sortUsingJava8(List<String> names) {
        Collections.sort(names, (s1,s2)->(s1.compareTo(s2)));
    }

    static class Car {
        public Car(String name) {
            this.name = name;
        }

        public Car() {
        }

        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}
